﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.Collections.ObjectModel;
using CommunitySystem.Ctrls;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView.晋升管理.新的晋升管理
{
    public partial class JSControl3 : UserControl
    {
        private JinShengService.JinShengServiceClient _client = null;
        private JinShengService.JinShen_ChooseCandidator_Main chooseMain = null;
        private JinShengService.JinShen_MainInfo _MainInfo = null;
        private ObservableCollection<JinShengService.JinShen_DangGongWei_Detail> details = new ObservableCollection<JinShengService.JinShen_DangGongWei_Detail>();


        public JSControl3(JinShen_MainInfo mainInfo, ObservableCollection<JinShengService.JinShen_DangGongWei_Detail> _details)
        {
            InitializeComponent();
            _MainInfo = mainInfo;
          //  details.Clear();
            details = _details;
          

         
            LoadUI();
        }
      
        private bool CanEditInfo = true;

        private void LoadUI()
        {
            if (_MainInfo.OrgID > 0 && string.IsNullOrEmpty(_MainInfo.Department))
            {
                LoadUIKS();
            }
            else
            {
                var des = details.Where(p => p.MainID == _MainInfo.ID).ToList();
                if (des != null && des.Count > 0)
                {
                    TextBlock tb = new TextBlock() { Text = "科室:" + _MainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                    selectionInfo.Children.Add(tb);
                    tb = new TextBlock() { Text = "职位:" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                    selectionInfo.Children.Add(tb);
                    foreach (var d in des)
                    {
                        Grid grid = new Grid() { Margin = new Thickness(3) };
                        Border b = new Border() { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.Blue), HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                        Grid.SetRow(b, 0);
                        Grid.SetRowSpan(b, 4);
                        grid.Children.Add(b);
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3), DataContext = d, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch };
                        Grid.SetRow(sp, 0);
                        tb = new TextBlock() { Text = "经党（工）委（组）", Margin = new Thickness(3) };
                        sp.Children.Add(tb);
                        DatePicker dp = new DatePicker();
                        //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree || CanEditInfo)
                        dp.IsEnabled = CanEditInfo;
                        dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("MeetingDate")
                        {
                            Mode = System.Windows.Data.BindingMode.TwoWay,
                            StringFormat = "yyyy-MM-dd"
                        });
                        sp.Children.Add(dp);
                        tb = new TextBlock() { Text = "日会议讨论决定如下：", Margin = new Thickness(3) };
                        sp.Children.Add(tb);
                        grid.Children.Add(sp);
                        sp = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                        tb = new TextBlock() { Text = d.EmpName, Margin = new Thickness(5, 6, 0, 3) };
                        DictComboBox comb = new DictComboBox() { DictCode = "IsAgree", Field = "IsAgree", DataContext = d, SelectedValuePath = "DictValue", DisplayMemberPath = "DictValue", Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0) };
                        //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                        comb.IsEnabled = CanEditInfo;
                        comb.SetBinding(DictComboBox.SelectedValueProperty, new System.Windows.Data.Binding("IsAgree") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        TextBlock tb2 = new TextBlock() { Text = "同志拟任" + _MainInfo.Department + _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };
                        sp.Children.Add(comb);
                        sp.Children.Add(tb);
                        sp.Children.Add(tb2);

                        //<local:DictTextBox  IsEnabled="False" x:Name="dtbPosition" Grid.Row="1"  Grid.Column="1"  Margin="8,4,8,2" HorizontalAlignment="Stretch" DictCode="zwdm" DictViewType="Name"  DictResultType="Name"  Field="Position"   VerticalAlignment="Stretch" />

                        tb2 = new TextBlock() { Text = "职位:", Margin = new Thickness(10, 6, 3, 3) };
                        DictTextBox dtb1 = new DictTextBox() { Field = "Position", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwdm", Margin = new Thickness(3), Width = 120, DataContext = d };
                        sp.Children.Add(tb2);
                        sp.Children.Add(dtb1);



                        tb2 = new TextBlock() { Text = "职务层次:", Margin = new Thickness(10, 6, 3, 3) };
                        DictTextBox dtb = new DictTextBox() { Field = "AttributLevel", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwcc", Margin = new Thickness(3), Width = 120, DataContext = d };
                        //tb = new TextBlock() { Text = "晋升为" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(3) };
                        dtb.IsEnabled = CanEditInfo;
                        sp.Children.Add(tb2);
                        sp.Children.Add(dtb);

                        //<local:DictTextBox x:Name="dtbPositionAttr" Width="120" Margin="5,8"  HorizontalAlignment="Stretch" DictCode="zwsx" DictViewType="Name"  DictResultType="Name"  Field="PositionAttibute"   VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>

                        tb2 = new TextBlock() { Text = "职位属性:", Margin = new Thickness(10, 6, 3, 3) };
                        DictTextBox dtb2 = new DictTextBox() { Field = "PositionAttibute", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwsx", Margin = new Thickness(3), Width = 120, DataContext = d };
                        sp.Children.Add(tb2);
                        sp.Children.Add(dtb2);




                        grid.Children.Add(sp);



                        StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                        tb2 = new TextBlock() { Text = "任期（年）:", Margin = new Thickness(3, 6, 3, 3) };
                        sp2.Children.Add(tb2);

                        TextBox textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                        textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("RenQi") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        textBox.IsEnabled = CanEditInfo;
                        sp2.Children.Add(textBox);

                        tb2 = new TextBlock() { Text = "是否有试用期:", Margin = new Thickness(10, 3, 0, 3) };
                        DictComboBox dcb = new DictComboBox() { Field = "HasShiYongQi", DisplayMemberPath = "DictValue", SelectedValuePath = "DictValue", DictCode = "sf", Margin = new Thickness(3), Width = 50, Height = 23, DataContext = d };
                        dcb.IsEnabled = CanEditInfo;
                        dcb.SelectionChanged += new SelectionChangedEventHandler(dcb_SelectionChanged);
                        StackPanel shiyongqi = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Center, Visibility = System.Windows.Visibility.Collapsed };
                        dcb.Tag = shiyongqi;
                        textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                        textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("ShiYongQiYear") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        textBox.IsEnabled = CanEditInfo;
                        TextBlock tb3 = new TextBlock() { Text = "试用期(年):", Margin = new Thickness(10, 3, 0, 3) };
                        shiyongqi.Children.Add(tb3);
                        shiyongqi.Children.Add(textBox);
                        sp2.Children.Add(tb2);

                        sp2.Children.Add(dcb);
                        sp2.Children.Add(shiyongqi);


                        Grid.SetRow(sp, 1);

                        tb = new TextBlock() { Text = "特此证明", HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Margin = new Thickness(3, 3, 100, 0) };
                        Grid.SetRow(tb, 3);
                        grid.Children.Add(tb);
                        detailsInfo.Children.Add(grid);
                    }
                }
                else
                    LayoutRoot.Visibility = Visibility.Collapsed;
            }

        }
        private ObservableCollection<BianZhiModel> ks = new ObservableCollection<BianZhiModel>();
        private void LoadUIKS()
        {
          

            var des = details.Where(p => p.MainID == _MainInfo.ID).ToList();
            if (des != null && des.Count > 0)
            {
                  if (_MainInfo.OrgID > 0 && string.IsNullOrEmpty(_MainInfo.Department))
            {
                EmpBasicServiceClient empclient = new EmpBasicServiceClient();
                 empclient.GetBianZhiModelAsync(_MainInfo.OrgID.ToString(), 0);
                 empclient.GetBianZhiModelCompleted += (s, e) =>
                 {
                     ks = e.Result;

                     TextBlock tb = new TextBlock() { Text = "科室:" + _MainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                     selectionInfo.Children.Add(tb);
                     tb = new TextBlock() { Text = "职位:" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                     selectionInfo.Children.Add(tb);
                     foreach (var d in des)
                     {
                         Grid grid = new Grid() { Margin = new Thickness(3) };
                         Border b = new Border() { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.Blue), HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                         Grid.SetRow(b, 0);
                         Grid.SetRowSpan(b, 4);
                         grid.Children.Add(b);
                         grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                         grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                         grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                         grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                         StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3), DataContext = d, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch };
                         Grid.SetRow(sp, 0);
                         tb = new TextBlock() { Text = "经党（工）委（组）", Margin = new Thickness(3) };
                         sp.Children.Add(tb);
                         DatePicker dp = new DatePicker();
                         //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree || CanEditInfo)
                         dp.IsEnabled = CanEditInfo;
                         dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("MeetingDate")
                         {
                             Mode = System.Windows.Data.BindingMode.TwoWay,
                             StringFormat = "yyyy-MM-dd"
                         });
                         sp.Children.Add(dp);
                         tb = new TextBlock() { Text = "日会议讨论决定如下：", Margin = new Thickness(3) };
                         sp.Children.Add(tb);
                         grid.Children.Add(sp);
                         sp = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                         tb = new TextBlock() { Text = d.EmpName, Margin = new Thickness(5, 6, 0, 3) };
                         DictComboBox comb = new DictComboBox() { DictCode = "IsAgree", Field = "IsAgree", DataContext = d, SelectedValuePath = "DictValue", DisplayMemberPath = "DictValue", Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0) };
                         //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                         comb.IsEnabled = CanEditInfo;
                         comb.SetBinding(DictComboBox.SelectedValueProperty, new System.Windows.Data.Binding("IsAgree") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        
                         
                         //TextBlock tb2 = new TextBlock() { Text = "同志拟任" + _MainInfo.Department + _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };
                         TextBlock tb2 = null;
                         TextBlock tb2s = null;
                         ComboBox cbks = null;

                         tb2s = new TextBlock() { Text = "同志拟任", Margin = new Thickness(0, 6, 3, 0) };
                         cbks = new ComboBox() { DisplayMemberPath = "JiGouMingCheng", Height = 25, Width = 100 };
                         cbks.SelectionChanged += (sender, ee) =>
                         {
                             if (((ComboBox)sender).SelectedItem != null)
                             {
                                 _MainInfo.Department = ((BianZhiModel)((((ComboBox)sender).SelectedItem))).JiGouMingCheng;
                             }

                         };
                         cbks.ItemsSource = ks;
                         tb2 = new TextBlock() { Text = _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };

                         
                         sp.Children.Add(comb);
                         sp.Children.Add(tb);
                         sp.Children.Add(tb2s);
                         sp.Children.Add(cbks);
                         sp.Children.Add(tb2);

                         //<local:DictTextBox  IsEnabled="False" x:Name="dtbPosition" Grid.Row="1"  Grid.Column="1"  Margin="8,4,8,2" HorizontalAlignment="Stretch" DictCode="zwdm" DictViewType="Name"  DictResultType="Name"  Field="Position"   VerticalAlignment="Stretch" />

                         tb2 = new TextBlock() { Text = "职位:", Margin = new Thickness(10, 6, 3, 3) };
                         DictTextBox dtb1 = new DictTextBox() { Field = "Position", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwdm", Margin = new Thickness(3), Width = 120, DataContext = d };
                         sp.Children.Add(tb2);
                         sp.Children.Add(dtb1);



                         tb2 = new TextBlock() { Text = "职务层次:", Margin = new Thickness(10, 6, 3, 3) };
                         DictTextBox dtb = new DictTextBox() { Field = "AttributLevel", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwcc", Margin = new Thickness(3), Width = 120, DataContext = d };
                         //tb = new TextBlock() { Text = "晋升为" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(3) };
                         dtb.IsEnabled = CanEditInfo;
                         sp.Children.Add(tb2);
                         sp.Children.Add(dtb);

                         //<local:DictTextBox x:Name="dtbPositionAttr" Width="120" Margin="5,8"  HorizontalAlignment="Stretch" DictCode="zwsx" DictViewType="Name"  DictResultType="Name"  Field="PositionAttibute"   VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>

                         tb2 = new TextBlock() { Text = "职位属性:", Margin = new Thickness(10, 6, 3, 3) };
                         DictTextBox dtb2 = new DictTextBox() { Field = "PositionAttibute", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwsx", Margin = new Thickness(3), Width = 120, DataContext = d };
                         sp.Children.Add(tb2);
                         sp.Children.Add(dtb2);




                         grid.Children.Add(sp);



                         StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                         tb2 = new TextBlock() { Text = "任期（年）:", Margin = new Thickness(3, 6, 3, 3) };
                         sp2.Children.Add(tb2);

                         TextBox textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                         textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("RenQi") { Mode = System.Windows.Data.BindingMode.TwoWay });
                         textBox.IsEnabled = CanEditInfo;
                         sp2.Children.Add(textBox);

                         tb2 = new TextBlock() { Text = "是否有试用期:", Margin = new Thickness(10, 3, 0, 3) };
                         DictComboBox dcb = new DictComboBox() { Field = "HasShiYongQi", DisplayMemberPath = "DictValue", SelectedValuePath = "DictValue", DictCode = "sf", Margin = new Thickness(3), Width = 50, Height = 23, DataContext = d };
                         dcb.IsEnabled = CanEditInfo;
                         dcb.SelectionChanged += new SelectionChangedEventHandler(dcb_SelectionChanged);
                         StackPanel shiyongqi = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Center, Visibility = System.Windows.Visibility.Collapsed };
                         dcb.Tag = shiyongqi;
                         textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                         textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("ShiYongQiYear") { Mode = System.Windows.Data.BindingMode.TwoWay });
                         textBox.IsEnabled = CanEditInfo;
                         TextBlock tb3 = new TextBlock() { Text = "试用期(年):", Margin = new Thickness(10, 3, 0, 3) };
                         shiyongqi.Children.Add(tb3);
                         shiyongqi.Children.Add(textBox);
                         sp2.Children.Add(tb2);

                         sp2.Children.Add(dcb);
                         sp2.Children.Add(shiyongqi);


                         Grid.SetRow(sp, 1);

                         tb = new TextBlock() { Text = "特此证明", HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Margin = new Thickness(3, 3, 100, 0) };
                         Grid.SetRow(tb, 3);
                         grid.Children.Add(tb);
                         detailsInfo.Children.Add(grid);
                     }
                 };
                  }
               
            }
            else
                LayoutRoot.Visibility = Visibility.Collapsed;

        }

        void dcb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var dcb = sender as DictComboBox;
            var detail = dcb.DataContext as JinShengService.JinShen_DangGongWei_Detail;
            var sp = dcb.Tag as StackPanel;
            if (dcb.SelectedValue != null && dcb.SelectedValue.Equals("是"))
            {
                sp.Visibility = System.Windows.Visibility.Visible;
            }
            else if (dcb.SelectedValue != null && dcb.SelectedValue.Equals("否"))
            {
                sp.Visibility = System.Windows.Visibility.Collapsed;
                detail.ShiYongQiYear = 0;
            }
        }

      
    }
}
